import { http } from "@/utils/http";
import { ComStatus } from "../model";

/**
 * ArticleQueryRequest
 */
export interface ArticleQueryRequest {
  /** 仅请求已发布文章 */ onlyPublished: boolean | null;
  /** 文章状态 */ status: ComStatus | null;
  /** 分类ID */ categoryId: number | null;
  search: string | null;
}

/**
 * ArticlePageData
 */
export interface ArticlePageData {
  rows: Array<Article>;
  total: number;
}

/**
 * Article
 */
export interface Article {
  id: number;
  title: string;
  slug: string;
  status: ComStatus;
  isPublish: boolean;
  summary: string;
  content: string;
  path: string;
  categoryId: number;
  category: ArticleCategory;
  categories: string;
  updateUserId: number;
  updateUserName: string;
  updateTime: string;
  createUserId: number;
  createUserName: string;
  createTime: string;
  type: ArticleType;
}

/**
 * ArticleCategory
 */
export interface ArticleCategory {
  id: number;
  name: string;
  parentId: number;
  parent: ArticleCategory;
  status: ComStatus;
  posts: Array<Article>;
  createUserId: number;
  createUserName: string;
  createTime: string;
}
export enum ArticleType {
  Article = 1,
  Notify = 2
}
/**
 * ArticleInfoResult
 */
export interface ArticleInfoResult {
  id: number;
  /** 标题 */ title: string;
  /** 文章链接，设置后可以通过以下形式访问文章 */ slug: string;
  /** 梗概 */ summary: string;
  /** 内容（markdown格式） */ content: string;
  /** 分类ID */ categoryId: number;
  status: ComStatus;
  type: ArticleType;
}
export type ArticleEditRequest = ArticleInfoResult;

export class ArticleApi {
  /**
   * 无
   */
  public list(
    query: {
      sort?: number;
      sortField?: string;
      page?: number;
      limit?: number;
    },
    param1?: ArticleQueryRequest
  ): Promise<ArticlePageData> {
    return http.request({
      url: "/api/article/list",
      method: "post",
      data: param1,
      params: {
        Sort: query.sort,
        SortField: query.sortField,
        Page: query.page,
        Limit: query.limit
      }
    });
  }
  public edit(param1?: ArticleEditRequest): Promise<void> {
    return http.request({
      url: "/api/article/edit",
      method: "post",
      data: param1
    });
  }
  public getInfo(id?: number): Promise<ArticleInfoResult> {
    return http.request({
      url: "/api/article/getInfo",
      method: "post",
      params: { id }
    });
  }
}
export default new ArticleApi();
